﻿using System.Collections;
using System.Timers;
using System;
using Timer = System.Timers.Timer;

namespace LinkedListDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            LinkedList linkedList = new LinkedList();
            linkedList.Add(1);
            linkedList.Add(2);
            linkedList.Add(3);
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }



            Timer timer = new Timer(100);
            timer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
            timer.Start();

            Console.ReadLine();
        }

        private static void OnTimerElapsed(object? sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Timer elapsed");
        }
    }

    public class LinkedListNode
    {
        public LinkedListNode(object value)
        {
            Value = value;
        }

        public object Value { get;  }

        public LinkedListNode Next { get; internal set; }

        public LinkedListNode Prev { get; internal set; }
    }

    public class LinkedList : IEnumerable
    {
        private LinkedListNode First { get; set; }

        private LinkedListNode Last { get; set; }

        public void Add(object value)
        {
            var node = new LinkedListNode(value);
            if (First == null)
            {
                First = node;
                Last = node;
            }
            else
            {
                LinkedListNode previous = Last;
                Last.Next = node;
                Last = node;
                node.Prev = previous;
            }
        }

        public IEnumerator GetEnumerator()
        {
            LinkedListNode current = First;
            while (current!= null)
            {
                yield return current.Value;
                current = current.Next;
            }
        }
    }
}
